package net_TCP.GQ;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {
    private Socket socket=null;
    public TCPClient (String serverIP,int serverPort) throws IOException {
        socket=new Socket(serverIP,serverPort);
    }
    public void start() throws IOException{
        Scanner scanner=new Scanner(System.in);
        try(InputStream inputStream=socket.getInputStream();
        OutputStream outputStream=socket.getOutputStream()) {
            while (true) {
                //1.输入请求
                System.out.println(">");
                String request=scanner.next();
                //2.将请求发送给服务器
                PrintWriter printWriter=new PrintWriter(outputStream);
                printWriter.println(request);
                printWriter.flush();
                //3.从服务器获取响应
                Scanner responseScan=new Scanner(inputStream);
                String response=responseScan.next();
                //4.展示在页面上
                System.out.println(response);

            }
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException{
        TCPClient tcpClient=new TCPClient("127.0.0.1",9090);
        tcpClient.start();
    }
}
